package com.gaogzhen.controller;

import com.gaogzhen.domain.Coin;
import com.gaogzhen.domain.CoinConfig;
import com.gaogzhen.model.ExceptionEnum;
import com.gaogzhen.service.CoinConfigService;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @version 1.0
 * @description
 * @date 2022-11-07 10:06
 */
@RestController
@RequestMapping("/coinConfigs")
@Api(tags = "币种配置控制器")
public class CoinConfigController {

    @Autowired
    private CoinConfigService coinConfigService;

    @GetMapping("/info/{coinId}")
    @ApiOperation(value = "查询币种配置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId", value = "币种id")
    })
    public R<CoinConfig> findByCoinId(@PathVariable("coinId") Long coinId) {
        return R.ok(coinConfigService.findByCoinId(coinId));
    }

    @PatchMapping
    @ApiOperation("修改币种配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinConfig", value = "coinConfig json数据")
    })
    public R update(@RequestBody @Validated CoinConfig coinConfig) {
        SqlExecuteUtils.sqlExceptionHandler(a ->coinConfigService.updateOrSave(a), coinConfig, ExceptionEnum.COIN_CONFIG_UPDATE_FAIL);

        return R.ok();
    }
}
